﻿
namespace HelfenHelfen.RIA.Web.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using HelfenHelfen.RIA.Web.Models;


    // Implements application logic using the HelfenHelfenEntities context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class HelfenHelfenDomainService : LinqToEntitiesDomainService<HelfenHelfenEntities>
    {

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with alternate signatures.
        // Alternatively, to mark this as the default parameterless query for this entity type, apply the
        // QueryAttribute custom attribute and set its IsDefault property true.
        public IQueryable<Demand> GetDemands()
        {
            return this.ObjectContext.Demands
                .OrderBy(d => d.DemandGUID);
        }

        public IQueryable<Demand> GetValidDemands()
        {
            return this.ObjectContext.Demands
                .Where(d => d.StartDateTime >= DateTime.Now)
                .OrderBy(d => d.DemandGUID);
        }

        public IQueryable<Demand> GetDemandByID(Guid ID)
        {
            return this.ObjectContext.Demands.Include("Images")
                .Where(d => d.DemandGUID == ID)
                .OrderBy(d => d.DemandGUID);
        }

        public void InsertDemand(Demand demand)
        {
            if ((demand.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(demand, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Demands.AddObject(demand);
            }
        }

        public void UpdateDemand(Demand currentDemand)
        {
            this.ObjectContext.Demands.AttachAsModified(currentDemand, this.ChangeSet.GetOriginal(currentDemand));
        }

        public void DeleteDemand(Demand demand)
        {
            if ((demand.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Demands.Attach(demand);
            }
            this.ObjectContext.Demands.DeleteObject(demand);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with alternate signatures.
        // Alternatively, to mark this as the default parameterless query for this entity type, apply the
        // QueryAttribute custom attribute and set its IsDefault property true.
        public IQueryable<DemandCategory> GetDemandCategories()
        {
            return this.ObjectContext.DemandCategories;
        }

        public IQueryable<Image> GetImages()
        {
            return this.ObjectContext.Images;
        }

        public void InsertImage(Image demandDetail)
        {
            if ((demandDetail.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(demandDetail, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Images.AddObject(demandDetail);
            }
        }

        public void UpdateImage(Image currentDemandDetail)
        {
            this.ObjectContext.Images.AttachAsModified(currentDemandDetail, this.ChangeSet.GetOriginal(currentDemandDetail));
        }

        public void DeleteImage(Image demandDetail)
        {
            if ((demandDetail.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Images.Attach(demandDetail);
            }
            this.ObjectContext.Images.DeleteObject(demandDetail);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with alternate signatures.
        // Alternatively, to mark this as the default parameterless query for this entity type, apply the
        // QueryAttribute custom attribute and set its IsDefault property true.
        public IQueryable<Organisation> GetOrganisations()
        {
            return this.ObjectContext.Organisations;
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with alternate signatures.
        // Alternatively, to mark this as the default parameterless query for this entity type, apply the
        // QueryAttribute custom attribute and set its IsDefault property true.
        public IQueryable<UserDetail> GetUserDetails()
        {
            return this.ObjectContext.UserDetails;
        }
    }
}


